package org.dromara.business.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.thirdparty.RedisIdGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

@Slf4j
@Service
@RequiredArgsConstructor
public class StakingRebateTaskService {

    private final TransactionTemplate transactionTemplate;
    private final RedisIdGenerator redisIdGenerator;

    /**
     * 执行质押返利
     */
    public void executeStakingRebate() {
//        // 获取待发放返利的质押产品
//        List<AppUserProduct> userProductList = appUserProductService.pendingDistribution();
//        if (CollUtil.isEmpty(userProductList)) {
//            return;
//        }
////        List<Long> productIdList = userProductList.stream()
////            .map(AppUserProduct::getProductId)
////            .collect(Collectors.toList());
////        Map<Long, AppProduct> productMap = appProductService.getMapByIds(productIdList);
//        for (AppUserProduct userProduct : userProductList) {
////            AppProduct product = productMap.get(userProduct.getProductId());
//            Integer residualDistributionCycle = userProduct.getResidualDistributionCycle();
//            AppUserProduct updateUserProduct = new AppUserProduct();
//            updateUserProduct.setId(userProduct.getId());
//            if (residualDistributionCycle == 0) {  // 判断用户剩余发放周期是否为0
//                log.info("==================该用户 {} 购买的质押循环贷 {} 发放周期已全部完成，不进行发放。", userProduct.getUserId(), userProduct.getProductId());
//                updateUserProduct.setStatus(2); // 状态设置为已结束
//                transactionTemplate.execute(status -> {
//                    appUserProductService.updateById(updateUserProduct);
//                    return true;
//                });
//                return;
//            }
//            // 获取购买的金额
//            BigDecimal purchaseAmount = userProduct.getPurchaseAmount();
//            // 获取收益率(%)
//            BigDecimal yieldRate = userProduct.getYieldRate();
//            // 计算用户此次返利金额
//            BigDecimal releaseAmount = NumberUtil.mul(purchaseAmount, NumberUtil.div(yieldRate, 100));
//            // 1.增加用户发放记录
//            AppProductReleaseRecord productReleaseRecord = new AppProductReleaseRecord();
//            productReleaseRecord.setUserId(userProduct.getUserId());
//            productReleaseRecord.setProductId(userProduct.getProductId());
//            productReleaseRecord.setReleaseTime(new Date());
//            productReleaseRecord.setReleaseAmount(releaseAmount);
//            productReleaseRecord.setRatio(yieldRate);
//            if (residualDistributionCycle == -1) { // 处理随进随出(24H)的返利
//                log.info("==================该用户 {} 购买的质押循环贷 {} 为随进随出", userProduct.getUserId(), userProduct.getProductId());
//                // 调用链进行处理，增加用户USDZ，链上处理成功后，返回txHash，然后修改发放状态
//                // 3.修改发放状态为已发放
//                transactionTemplate.execute(status -> {
////                    appUserProductService.updateById(updateUserProduct);
//                    appProductReleaseRecordService.save(productReleaseRecord);
//                    return true;
//                });
//            } else {
//                transactionTemplate.execute(status -> {
//                    appUserProductService.update(userProduct.getId(), releaseAmount);
//                    appProductReleaseRecordService.save(productReleaseRecord);
//                    return true;
//                });
//            }
//        }
    }
}
